package com.xjoyt.pay.master.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.commons.ObjectUtils;
import com.xjoyt.pay.commons.PayTypeUtils;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.params.MerchantPageParam;
import com.xjoyt.pay.master.po.Merchant;
import com.xjoyt.pay.master.po.MerchantCashRate;
import com.xjoyt.pay.master.po.MerchantRate;
import com.xjoyt.pay.master.repository.MerchantCashRateRepository;
import com.xjoyt.pay.master.repository.MerchantRateRepository;
import com.xjoyt.pay.master.repository.MerchantRepository;
import com.xjoyt.pay.master.vo.MerchantRateMap;
import com.xjoyt.pay.master.vo.RateEntry;
import com.xjoyt.pay.vo.PayType;

@Service
public class MerchantRateService extends AbstractService {

	@Autowired
	private MerchantRateRepository repository;

	@Autowired
	private MerchantRepository merchantRepository;

	@Autowired
	private MerchantCashRateRepository cashRepository;

	public PageResult<MerchantRateMap> loadPage(PageParam<MerchantPageParam> param) {
		PageResult<Merchant> page = merchantRepository.loadPage(param);
		List<MerchantRateMap> rows = new ArrayList<>();

		List<String> ids = new ArrayList<>();
		page.forEach(item -> {
			ids.add(item.id);
			MerchantRateMap vo = new MerchantRateMap();
			rows.add(vo);
			ObjectUtils.fieldCopy(item, vo);
		});
		List<MerchantRate> list = CollectionUtils.isEmpty(ids) ? new ArrayList<>()
				: repository.findAllByMerchantIdIn(ids);
		List<MerchantCashRate> cashList = CollectionUtils.isEmpty(ids) ? new ArrayList<>()
				: cashRepository.findAllByMerchantIdIn(ids);
		rows.forEach(item -> {
			item.rateList = new ArrayList<>();
			for (PayType type : PayType.values()) {
				RateEntry entry = new RateEntry();
				entry.type = type;
				item.rateList.add(entry);
				entry.name = PayTypeUtils.getName(type);
				for (MerchantRate merchantRate : list) {
					if (merchantRate.money == null || merchantRate.limit == null || merchantRate.minimum == null) 
						continue;
					
					if (type.equals(merchantRate.type) && item.id.equals(merchantRate.merchant.id)) {
						entry.rate = merchantRate.rate;
						entry.money = merchantRate.money;
						entry.limit = merchantRate.limit;
						entry.minimum = merchantRate.minimum;
						break;
					}
				}
			}

			if (CollectionUtils.isNotEmpty(cashList)) {
				for (MerchantCashRate merchantCashRate : cashList) {
					if (item.id.equals(merchantCashRate.merchant.id)) {
						item.cashRate = merchantCashRate.rate;
						item.cashMoney = merchantCashRate.money;
						item.cashLimit = merchantCashRate.limit;
						item.cashMinimum = merchantCashRate.minimum;
					}
				}
			}
		});

		return new PageResult<>(page.getTotal(), rows);
	}

	public ExecuteResult<?> saveRate(MerchantRateMap param) {
		Merchant merchant = merchantRepository.findOne(param.id);
		if (merchant == null) {
			return ExecuteResult.fail("数据不存在");
		}
		repository.findAllByMerchantId(param.id).forEach(item -> repository.delete(item));
		if (CollectionUtils.isEmpty(param.rateList)) {
			return ExecuteResult.ok();
		}

		for (RateEntry item : param.rateList) {
			if (StringUtils.isBlank(PayTypeUtils.getName(item.type)))
				continue;
			MerchantRate entity = new MerchantRate();
			entity.type = item.type;
			entity.rate = item.rate;
			entity.money = item.money;
			entity.limit = item.limit;
			entity.minimum = item.minimum;
			entity.merchant = merchant;
			repository.save(entity);
		}

		if (String.valueOf(param.cashRate) != null || String.valueOf(param.cashMoney) != null
				|| String.valueOf(param.cashLimit) != null || String.valueOf(param.cashMinimum) != null) {
			MerchantCashRate merchantCashRate = new MerchantCashRate();
			merchantCashRate.merchant = merchant;
			merchantCashRate.rate = param.cashRate;
			merchantCashRate.money = param.cashMoney;
			merchantCashRate.limit = param.cashLimit;
			merchantCashRate.minimum = param.cashMinimum;
			cashRepository.save(merchantCashRate);
		}

		return ExecuteResult.ok();
	}

	public ExecuteResult<MerchantRate> save(MerchantRate param) {
		if (param.merchant == null || StringUtils.isBlank(param.merchant.id)) {
			return ExecuteResult.fail("数据不存在");
		}
		repository.findAllByMerchantIdAndType(param.merchant.id, param.type).forEach(item -> {
			repository.delete(item);
		});
		param.id = null;
		MerchantRate entity = repository.save(param);
		return ExecuteResult.ok(entity);
	}

	public ExecuteResult<?> update(com.xjoyt.pay.merchant.register.po.MerchantRate param) {
		MerchantRateMap merchantRateMap = new MerchantRateMap();
		merchantRateMap.id = param.merchant.id;
		List<RateEntry> rateList = new ArrayList<RateEntry>();
		param.rateList.forEach(item -> {
			RateEntry entry = new RateEntry();
			entry.type = item.type;
			entry.rate = item.rate;
			rateList.add(entry);
		});
		merchantRateMap.rateList = rateList;
		saveRate(merchantRateMap);
		return ExecuteResult.ok();
	}

}
